﻿using System;
using System.Web.UI.WebControls;
using ISHotelApp.Database;

namespace ISHotel_ASP.presentation
{
    public partial class HostForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonVlozHost_Click(object sender, EventArgs e)
        {
            Host newHost = new Host();
            newHost.rodne_cislo = TextBoxRodneCislo.Text;
            newHost.jmeno = TextBoxJmeno.Text;
            newHost.prijmeni = TextBoxPrijmeni.Text;
            newHost.ulice = TextBoxUlice.Text;
            newHost.mesto = TextBoxMesto.Text;
            newHost.psc = Convert.ToInt32(TextBoxPSC.Text);
            if (TextBoxTelefon.Text == "")
                newHost.telefon = 0;
            else
                newHost.telefon = Convert.ToInt32(TextBoxTelefon.Text);
            if (TextBoxTelefon.Text == "")
                newHost.email = "NOT PROVIDED";
            else
                newHost.email = TextBoxEmail.Text;
            HostTable addSourceRecord = new HostTable();

            try
            {
                addSourceRecord.Insert(newHost);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString().IndexOf("ORA-00001") >= 0) { LabelErr.Text = "Host s tímto rodným číslem již existuje."; }
                else { LabelErr.Text = ex.Message.ToString(); };
            }
        }

        public void button_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "DeleteHost")
            {
                HostTable delSourceRecord = new HostTable();
                
                try
                {
                    delSourceRecord.Delete(Convert.ToString(e.CommandArgument));
                }
                catch (Exception ex)
                {
                    LabelErr.Text = "Host nelze smazat, má již vytvořeny objednávky.";
                }
                if (LabelErr.Text == "")
                {
                    Response.Redirect(Request.RawUrl);
                }
            }
        }


        protected void HostDetail_OnUpdated(object sender, EventArgs e)
        {
            GridViewHost.DataBind();
            DetailsView1.DataBind();
        }
    }
}